var socketIo=function(server){
    var io  = require('socket.io').listen(server);
    return  io;
}

var getTime=function(){
    var date = new Date();
    //console.log(date.get());
    return date.getFullYear()+"-"+date.getMonth()+"-"+date.getDay()+" "
        +date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
}
var client = {
    socket:socketIo,
    form_user_id:0,
    form_user_name:'',
    form_user_type:'',
    to_user_id:0,
    to_user_type:'helper',
    message:'',
    time:getTime()
};
var socketOpen=function(socketIo){
    socketIo.on('connection',function(socket){
        socket.emit('open',{});
        socket.on('message', function(msg){
            console.log(msg);
            if(msg.user_type=='public'){
                client.form_user_id=users.user_id;
                client.form_user_name=users.user_name;
                client.form_user_type=users.user_type;
                client.to_user_id=helper.user_id;
                client.to_user_type=helper.user_type;  //扩展用
                client.message=msg.msg;
                client.time=getTime();
                console.log('this user');
                console.log(users);
            }else{
                client.form_user_id=helper.user_id;
                client.form_user_name=helper.user_name;
                client.form_user_type=helper.user_type;
                client.to_user_id=users.user_id;
                client.to_user_name=users.user_name;
                client.to_user_type=users.user_type;
                client.message=msg.msg;
                client.time=getTime();
            }
            socketIo.emit('messages',client);
        });

    })
}
exports.socketIo=socketIo;
exports.socketOpen=socketOpen;


